import axios from "axios";
// import { message } from "antd";
// const [messageApi] = message.useMessage();
const instance = axios.create({
  baseURL: "http://blogapi.shbwyz.com/",
  timeout: 3000,
});
// 添加请求拦截器
instance.interceptors.request.use(
  function (config) {
    // 在发送请求之前做些什么
    config.headers.Authorization = "Bearer " + localStorage.getItem("token");
    // messageApi.open({
    //   type: "loading",
    //   content: "Action in progress..",
    //   duration: 0,
    // });
    // Dismiss manually and asynchronously
    // setTimeout(messageApi.destroy, 2500);
    return config;
  },
  function (error) {
    // 对请求错误做些什么
    // messageApi.open({
    //   type: "loading",
    //   content: "Action in progress..",
    //   duration: 0,
    // });
    // Dismiss manually and asynchronously
    // setTimeout(messageApi.destroy, 2500);
    return Promise.reject(error);
  }
);

// 添加响应拦截器
instance.interceptors.response.use(
  function (response) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    // messageApi.open({
    //   type: "loading",
    //   content: "Action in progress..",
    //   duration: 0,
    // });
    // Dismiss manually and asynchronously
    // setTimeout(messageApi.destroy, 2500);
    return response.data;
  },
  function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    // messageApi.open({
    //   type: "loading",
    //   content: "Action in progress..",
    //   duration: 0,
    // });
    // Dismiss manually and asynchronously
    // setTimeout(messageApi.destroy, 2500);
    if (error.response.status !== 200) {
      if (error.response.status === 300) {
        console.log("300报错");
      } else if (error.response.status === 304) {
        console.log("304报错");
      } else if (error.response.status === 400) {
        console.log("400报错");
      } else if (error.response.status === 401) {
        console.log("401报错");
      } else if (error.response.status === 404) {
        console.log("404报错");
      } else if (error.response.status === 500) {
        console.log("500报错");
      }
    }
    return Promise.reject(error);
  }
);
export default instance;
